package com.jerry.mingxing.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jerry.mingxing.R;
import com.jerry.mingxing.ui.categroy.CategroyFragment;
import com.jerry.mingxing.ui.fashion.FashionFragment;
import com.jerry.mingxing.ui.message.MessageFragment;
import com.jerry.mingxing.ui.my.MyFragment;
import com.jerry.mingxing.ui.store.StoreFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.main_flt)
    FrameLayout mainFlt;
    @Bind(R.id.main_rbtn_store)
    RadioButton mainRbtnStore;
    @Bind(R.id.main_rbtn_categroy)
    RadioButton mainRbtnCategroy;
    @Bind(R.id.main_rbtn_fasion)
    RadioButton mainRbtnFasion;
    @Bind(R.id.main_rbtn_message)
    RadioButton mainRbtnMessage;
    @Bind(R.id.main_rbtn_my)
    RadioButton mainRbtnMy;
    @Bind(R.id.main_rgp)
    RadioGroup mainRgp;
    private StoreFragment storeFragment;
    private CategroyFragment categroyFragment;
    private FashionFragment fashionFragment;
    private MessageFragment messageFragment;
    private MyFragment myFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //黄油刀绑定控件

        initView();
        setListener();


    }

    private void setListener() {
        mainRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.main_rbtn_store:
                        switchFragment(0);
                        break;
                    case R.id.main_rbtn_categroy:
                        switchFragment(1);
                        break;
                    case R.id.main_rbtn_fasion:
                        switchFragment(2);
                        break;
                    case R.id.main_rbtn_message:
                        switchFragment(3);
                        break;
                    case R.id.main_rbtn_my:
                        switchFragment(4);
                        break;
                }
            }
        });
    }

    private void initView() {
        ButterKnife.bind(this);
        manager = getSupportFragmentManager();
        //默认加载时尚的
        switchFragment(2);
    }
    //切换碎片的方法,根据索引对碎片进行切换
    public void switchFragment(int index){
        //创建碎片管理器
        FragmentTransaction transaction = manager.beginTransaction();
        isAddTransaction(transaction);

        //根据id对碎片进行切换 ,如果碎片开始为空 进行创建
        switch (index){
            case 0:
                if (storeFragment==null){
                    storeFragment= new StoreFragment();
                    transaction.add(R.id.main_flt,storeFragment);
                }else {
                    //把如果碎片存在直接让他展示
                    transaction.show(storeFragment);
                }

                break;
            case 1:
                if (categroyFragment==null){
                    categroyFragment= new CategroyFragment();
                    transaction.add(R.id.main_flt,categroyFragment);
                }else {

                    transaction.show(categroyFragment);
                }

                break;
            case 2:
                if (fashionFragment==null){
                    fashionFragment= new FashionFragment();
                    transaction.add(R.id.main_flt,fashionFragment);
                }else {

                    transaction.show(fashionFragment);
                }

                break;
            case 3:
                if (messageFragment==null){
                    messageFragment= new MessageFragment();
                    transaction.add(R.id.main_flt,messageFragment);
                }else {

                    transaction.show(messageFragment);
                }
                break;
            case 4:
                if (myFragment==null){
                    myFragment= new MyFragment();
                    transaction.add(R.id.main_flt,myFragment);
                }else {

                    transaction.show(myFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void isAddTransaction(FragmentTransaction transaction) {
        if (storeFragment!=null){
            transaction.hide(storeFragment) ;
        }
        if (categroyFragment!=null){
            transaction.hide(categroyFragment) ;
        }
        if (fashionFragment!=null){
            transaction.hide(fashionFragment) ;
        }
        if (messageFragment!=null){
            transaction.hide(messageFragment) ;
        }
        if (myFragment!=null){
            transaction.hide(myFragment) ;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
